/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateChangePlaneView.java
 *
 * Created on 15-jan-2010, 14:39:41
 */
package View;

import Model.Plane;
import Util.StringToHtmlConverter;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Jeroen
 */
public class CreateChangePlaneView extends javax.swing.JInternalFrame {

    private Plane plane = null;
    private boolean newPlane;

    /**
     * Creates new form CreateChangePlaneView
     */
    public CreateChangePlaneView(Plane plane) {
        initComponents();
        this.setTitle("Add Plane");

        if (plane != null) {
            this.plane = plane;
            this.newPlane = false;
            this.setTitle("Change Plane");
            txtNumber.setText("" + plane.getNumber());
            txtCapacity.setText("" + plane.getCapacity());
            txtType.setText(plane.getType());
        } else {
            this.plane = new Plane();
            this.newPlane = true;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNumber = new javax.swing.JLabel();
        lblType = new javax.swing.JLabel();
        lblCapacity = new javax.swing.JLabel();
        txtNumber = new javax.swing.JTextField();
        txtType = new javax.swing.JTextField();
        txtCapacity = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblError = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setName("Form"); // NOI18N
        setOpaque(true);
        setRequestFocusEnabled(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(flyaway.FlyAWayApp.class).getContext().getResourceMap(CreateChangePlaneView.class);
        lblNumber.setText(resourceMap.getString("lblNumber.text")); // NOI18N
        lblNumber.setName("lblNumber"); // NOI18N

        lblType.setText(resourceMap.getString("lblType.text")); // NOI18N
        lblType.setName("lblType"); // NOI18N

        lblCapacity.setText(resourceMap.getString("lblCapacity.text")); // NOI18N
        lblCapacity.setName("lblCapacity"); // NOI18N

        txtNumber.setText(resourceMap.getString("txtNumber.text")); // NOI18N
        txtNumber.setName("txtNumber"); // NOI18N

        txtType.setName("txtType"); // NOI18N

        txtCapacity.setName("txtCapacity"); // NOI18N

        btnSave.setText(resourceMap.getString("btnSave.text")); // NOI18N
        btnSave.setName("btnSave"); // NOI18N
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnCancel.setText(resourceMap.getString("btnCancel.text")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        lblError.setText(resourceMap.getString("lblError.text")); // NOI18N
        lblError.setName("lblError"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCapacity)
                            .addComponent(lblType)
                            .addComponent(lblNumber))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtType, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNumber)
                            .addComponent(txtCapacity)))
                    .addComponent(lblError, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lblCapacity, lblNumber, lblType});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnSave});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNumber)
                    .addComponent(txtNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblType)
                    .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCapacity)
                    .addComponent(txtCapacity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnCancel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblError, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {txtCapacity, txtNumber, txtType});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblCapacity, lblNumber, lblType});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean savePlane() {
        String type = txtType.getText();
        Integer number = null;
        Integer capacity = null;
        ArrayList<String> errorMessages = new ArrayList<>();

        type = type.trim();

        if (type.isEmpty()) {
            errorMessages.add("Please fill in a type");
        }

        if (txtCapacity.getText().isEmpty()) {
            errorMessages.add("Please fill in capacity");
        } else {
            try {
                capacity = Integer.parseInt(txtCapacity.getText());
                if (capacity < 0) {
                    errorMessages.add("Capacity is below zero");
                }
            } catch (NumberFormatException e) {
                errorMessages.add("Capacity isn't a number");
            }
        }

        if (txtNumber.getText().isEmpty()) {
            errorMessages.add("Please fill in a number");
        } else {
            try {
                number = Integer.parseInt(txtNumber.getText());
                if (number < 0) {
                    errorMessages.add("Plane Number is below zero");
                }
            } catch (NumberFormatException e) {
                errorMessages.add("Plane Number isn't a number");
            }
        }

        if (!errorMessages.isEmpty()) {
            lblError.setText(StringToHtmlConverter.format(errorMessages));
            return false;
        }

        if (newPlane) {
            plane.setNumber(number);
            plane.setType(type);
            plane.setCapacity(capacity);
            if (Controller.Controller.Instance().save(plane)) {
                JOptionPane.showMessageDialog(this, "Plane " + plane.getNumber() + " saved");
                return true;
            } else {
                lblError.setText("Unable to save plane");
            }
        } else {
            plane.setNumber(number);
            plane.setType(type);
            plane.setCapacity(capacity);

            if (Controller.Controller.Instance().update(plane)) {
                JOptionPane.showMessageDialog(this, "Plane " + plane.getNumber() + " saved");
                return true;
            } else {
                lblError.setText("Unable to change plane");
            }
        }
        return false;
    }

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (this.savePlane()) {
            this.dispose();
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel lblCapacity;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblNumber;
    private javax.swing.JLabel lblType;
    private javax.swing.JTextField txtCapacity;
    private javax.swing.JTextField txtNumber;
    private javax.swing.JTextField txtType;
    // End of variables declaration//GEN-END:variables
}
